/*

2015-09-15

插件版本1.1：
改变了按钮点击事件变为mouseenter，动画更加流畅


*/

(function($) {
    var num = 1;
    $.fn.animateT = function(options){
        // 默认属性
        var defaults = {      
            speed: 200,
            pause: 5000,
            auto: false,
            arrow: true,
            disc: true,
            full: false,
            animateTShell: 1200
        }
        var options = $.extend(defaults, options);
      
        this.each(function() {
            var obj = $(this);
            var s = $("li.ani_li",obj).length;
            var h = $("li.ani_li",obj).height();
            var clickable = true;
            var ts = s-1;
            var t = 0;
            var disc = '';
            var dsum = 0;
            var str = '';
            var startX = 0,startY = 0;
            var endX = 0,endY = 0;
            var movePrevent = false;
            var stopMove = false;
            //如果全屏模式用100%，否则动态赋值
            if(options.full){
                var w = document.body.clientWidth;
                $("li.ani_li",obj).css("width",w);
                $("li.ani_li",obj).parents('.animateTShell').css("width",100+"%");
            }else{
                $("li.ani_li",obj).parents('.animateTShell').css("width",options.animateTShell);
                var w = $("li.ani_li",obj).width();
                $("li.ani_li",obj).css("width",w);
            } 

            $(window).resize(function(){
                if(options.full){
                    w = document.body.clientWidth;
                     $("li.ani_li",obj).css("width",w);
                     clearTimeout(timeout);
                     stopMove = true;
                     $("ul.ani_ul",obj).css("margin-left",(Math.abs(t)-1)*-w);
                     $("ul.ani_ul li.ani_li:first-child",obj).css("margin-left","-"+ w +"px");
                     timeout = setTimeout(function(){
                        animate("right",false);
                        stopMove = false;
                    },options.pause);
                }    
            });
            
            $("li.ani_li",obj).parent().parent().mouseenter(function(event) {
                clearTimeout(timeout);
                stopMove = true;
                // console.log("清除");
            }).mouseleave(function(event) {
                if(options.auto){
                    timeout = setTimeout(function(){
                        animate("right",false);
                    },options.pause);
                    stopMove = false;
                }
                // console.log("继续");
            });

            if(s>1){
                //左右箭头自动添加
                if(options.arrow){
                    obj.parent().append('<a href="javascript:void(0);" class="arrowLeft"></a><a href="javascript:void(0);" class="arrowRight"></a>');
                }
                //添加首和尾部元素
                $('ul.ani_ul li.ani_li:last-child',obj).clone().prependTo("#animateT"+num+" ul.ani_ul").css("margin-left","-"+ w +"px");
                $('ul.ani_ul li.ani_li:nth-child(2)',obj).clone().appendTo("#animateT"+num+" ul.ani_ul");
                $("ul.ani_ul",obj).width((s+2)*w);
                num++;
            }
            
            //选择按钮自动添加
            if(options.disc){
                for(var i = 0;i<s;i++){
                    if(s == 1){
                        disc = '';
                        $(".arrowLeft").hide();
                        $(".arrowRight").hide();
                    }else if(dsum == 0){
                        disc += '<span class="disc disc_hover" number='+dsum+'></span>'
                        dsum++;
                    }else if(dsum > 0){
                        disc += '<span class="disc" number='+dsum+'></span>'
                        dsum++;
                    }
                }
                str = '<span class="disc_border">'+disc+'</span>';
                obj.append(str);
            }          

            //左右移动click
            $(".arrowLeft",obj.parent()).click(function() {
                animate("left",true);
            });
            $(".arrowRight",obj.parent()).click(function() {
                animate("right",true);
            });
            $(".disc",obj).mouseenter(function() {
                animatedisc(-parseInt($(this).attr("number")));
            });

            //鼠标拖动
            // obj[0].addEventListener('mousedown', function (e) {
            //     onStart(e);
            // });
            // obj[0].addEventListener('mousemove', function (e) {
            //     onMove(e);
            // });
            // obj[0].addEventListener('mouseup', function (e) {
            //     onEnd(e);
            // });
            // obj[0].addEventListener('touchstart', function (e) {
            //     e = e.changedTouches[0];
            //     onStart(e);
            // });
            // obj[0].addEventListener('touchmove', function (e) {
            //     onMove(e.changedTouches[0], e);
            // });           
            // obj[0].addEventListener('touchend', function (e) {
            //     onEnd(e.changedTouches[0]);
            // });
            function onStart(e){
                // console.log("movePrevent=="+movePrevent);
                if(movePrevent == true){
                    event.preventDefault();
                    return false;
                }
                event.preventDefault();
                // 起始点，页面位置
                startX = e.pageX;
                startY = e.pageY;
            }
            function onMove(e){
                if(movePrevent == true){
                    event.preventDefault();
                    return false;
                }
                event.preventDefault();
                
            }
            function onEnd(e){
                if(movePrevent == true){
                    event.preventDefault();
                    return false;
                }
                endX = e.pageX;
                // console.log('startX=='+startX+'endX=='+endX);
                event.preventDefault();
                if(endX-startX>100){
                    // console.log("left");
                    animate("left",true);
                }
                if(endX-startX<-100){
                    // console.log("right");
                    animate("right",true);
                }
            }

            function animate(dir,clicked){
                if(clickable){
                    clickable = false;
                    var p = null;
                    switch(dir){
                        case "left" :
                            t++;
                            break;
                        case "right" :
                            t--;
                            break;
                        default:
                            t = dir;
                            break;
                    }

                    var speed = options.speed;
                    p = (t*w);
                    $("ul.ani_ul",obj).animate(
                        { marginLeft: p }, 
                        { queue:false, duration:speed, complete:adjust}
                    );

                    if(clicked) clearTimeout(timeout);
                    // console.log("options.auto==="+options.auto+"s==="+s+"stopMove==="+stopMove);
                    if(options.auto && s>1 && stopMove == false){
                        timeout = setTimeout(function(){
                            animate("right",false);
                        },options.pause);
                        // console.log("继续2");
                    }
                }
            }
            function animatedisc(dir){
                    clickable = false;
                    t = dir;
                    var speed = options.speed;
                    p = (t*w);
                    $("ul.ani_ul",obj).stop().animate(
                        { marginLeft: p }, 
                        { queue:false, duration:speed,complete:adjustdisc}
                    );
                    $(".disc",obj).attr("class","disc");
                    $('.disc_border span[number='+(-t)+']',obj).addClass('disc_hover');
                
            }
            function adjust(){
                if(t==1){
                    $("ul.ani_ul",obj).css("marginLeft",(s-1)*-w);
                    t = -ts;
                }
                if(t<-ts){
                    $("ul.ani_ul",obj).css("marginLeft",0);
                    t = 0;
                }
                $(".disc",obj).attr("class","disc");
                $('.disc_border span[number='+(-t)+']',obj).addClass('disc_hover');
                clickable = true;
                setTimeout("movePrevent=false;", 300 );
            }
            function adjustdisc(){
                clickable = true;
            }

            // init
            var timeout;
            if(options.auto && s>1){;
                timeout = setTimeout(function(){
                    animate("right",false);
                },options.pause);
            };
            
        }); 
    }
})(jQ);